<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\logic\category\admin\Category;
use app\common\service\BaseService;
use app\exam\logic\admin\ModuleRollup as ModuleRollupLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;
use think\Response;

/**
 * Class ModuleRollup
 *
 * @package app\exam\service\admin
 */
class ModuleRollup
{
    /**
     * @Inject()
     *
     * @var ModuleRollupLogic
     */
    protected $moduleRollupLogic;

    /**
     * 套卷配置
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/1
     *
     * @param int $moduleId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DbException
     */
    public function config(int $moduleId)
    {
        $map  = ['module_id' => $moduleId];
        $data = $this->moduleRollupLogic->getList($map);

        // 定义字段
        $keyList = [
            'title'         => ListItem::make()->title('套卷名称'),
            'paper_count'   => ListItem::make()->title('试卷数'),
            'price'         => ListItem::make('custom')->title('价格'),
            'status'        => ListItem::make('custom')->title('状态'),
            // 'credit_redeem' => ListItem::make('custom')->title('积分抵扣购买'),
            'update_time'   => ListItem::make()->title('更新时间'),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 创建套卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/1
     *
     * @param int $moduleId
     * @return \eduline\admin\page\interfaces\think\Response|\think\Response
     */
    public function create(int $moduleId)
    {
        if (Request::isPost()) {
            $data = Request::post();
            $this->moduleRollupLogic->createRollup($moduleId, $data);

            return BaseService::parseToData([], 1, '新建成功');
        }
        /* 创建课程界面配置 */
        $keyList = $this->keyListInit(true);
        // 设置表单
        $form = new PageForm();
        // 表单字段
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'POST';
        $form->savePostUrl = '/exam/admin/module/' . $moduleId . '/rollup/create';

        return $form->send();
    }

    /**
     * 编辑套卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/1
     *
     * @param int $moduleId
     * @param int $rollupId
     * @return \eduline\admin\page\interfaces\think\Response|\think\Response
     */
    public function edit(int $moduleId, int $rollupId)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $this->moduleRollupLogic->editRollup($rollupId, $moduleId, $data);

            return BaseService::parseToData([], 1, '编辑成功');
        }

        $data = $this->moduleRollupLogic->getRollupById($rollupId);
        $data = $data->toArray();
        $data['category'] = $data['fullcategorypath'];
        // 创建课程界面配置
        $keyList = $this->keyListInit(!$data['paper_count']);
        // 设置表单
        $form = new PageForm();
        // 表单字段
        $form->datas       = $data;
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/exam/admin/module/' . $moduleId . '/rollup/' . $rollupId . '/edit';

        return $form->send();
    }

    /**
     * 表单字段
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/1
     *
     * @return array
     */
    public function keyListInit($visible = false)
    {
        /** @var Category $cateLogic */
        $cateLogic = app(Category::class);
        $category  = $cateLogic->cascader(0, 3);
        // 定义字段
        $keyList = [
            'category'              => FormItem::make('cascader')->title('套卷分类')->visible($visible)->options($category)->required(),
            'title'                 => FormItem::make()->title('套卷名称')->maxlength(50)->required(),
            'intro'                 => FormItem::make('Textarea')->title('简介')->maxlength(150)->wordLimit(),
            'price'                 => FormItem::make('inputNumber')->title('价格')->min(0)->precision(2)->required(),
            'credit_redeem'         => FormItem::make('slideSwitch')->title('积分抵扣购买')->activeValue(1)->inactiveValue(0)->required(),
            'credit_redeem_percent' => FormItem::make('inputNumber')->title('积分抵扣比例')->min(0)->max(100)->required(),
        ];

        return $keyList;
    }

    /**
     * 编辑某个字段
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/2
     *
     * @param int    $rollupId
     * @param string $field
     * @return Response
     */
    public function editField(int $rollupId, string $field)
    {
        $value = Request::put($field);

        $this->moduleRollupLogic->editRollupField($rollupId, $field, $value);

        return BaseService::parseToData([], 1, '编辑成功');
    }

    /**
     * 删除
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/2
     *
     * @param int $rollupId
     * @return Response
     */
    public function delete(int $rollupId)
    {
        $this->moduleRollupLogic->deleteRollup($rollupId);

        return BaseService::parseToData([], 1, '删除成功');
    }
}